package com.free.beanutils;

import com.free.exception.SystemException;
import com.free.utils.PropertiesResourceUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
public class BeanUtilsHelper {
    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/
    private static final BeanUtilsHelper _instance = new BeanUtilsHelper();

    private String basePackages;

    private String[] basePackageNames;

    private boolean propertyNameUnderLine = false;

    private static final Object lock = new Object();
    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/
    BeanUtilsHelper(){
        try {
            Properties properties = PropertiesResourceUtils.load("beanutils.properties");
            basePackages = PropertiesResourceUtils.getPropertyValue(properties,"basePackages");
            String flg = PropertiesResourceUtils.getPropertyValue(properties, "propertyNameUnderLine");
            if(StringUtils.isNotEmpty(flg) && StringUtils.equals(flg,"true")){
                propertyNameUnderLine = true;
            }
        } catch (SystemException e) {
            e.printStackTrace();
        }
    }
    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    public static BeanUtilsHelper instance(){
        return _instance;
    }

    public String getBasePackages() {
        return basePackages;
    }

    public void setBasePackages(String basePackages) {
        this.basePackages = basePackages;
    }

    public boolean isPropertyNameUnderLine() {
        return propertyNameUnderLine;
    }

    public void setPropertyNameUnderLine(boolean propertyNameUnderLine) {
        this.propertyNameUnderLine = propertyNameUnderLine;
    }

    public void setBasePackageNames(){
        if(StringUtils.isEmpty(basePackages)){
            basePackageNames = null;
        }
        basePackageNames = StringUtils.split(basePackages,",");
    }

    public boolean isInPackages(String clazzName) {
        if(basePackageNames == null){
            return false;
        }
        boolean flg = false;
        for(String baseP : basePackageNames){
            if(clazzName.indexOf(baseP) >= 0){
                flg = true;
            }
        }
        return flg;
    }

    public String transferPropertyName(String name){
        if(propertyNameUnderLine){
            if(name.indexOf("_") >= 0){
                return underline2Camel(name, true);
            }
            return name;
        }
        return name;
    }

    public String underline2Camel(String line, boolean ... smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
        Matcher matcher = pattern.matcher(line);
        //匹配正则表达式
        while (matcher.find()) {
            String word = matcher.group();
            //当是true 或则是空的情况
            if((smallCamel.length ==0 || smallCamel[0] ) && matcher.start()==0){
                sb.append(Character.toLowerCase(word.charAt(0)));
            }else{
                sb.append(Character.toUpperCase(word.charAt(0)));
            }

            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
